  /**
实现 pow(x, n) ，即计算 x 的 n 次幂函数（即，x⁴

 Related Topics 递归 数学 👍 828 👎 0

*/

package medium._0050.pck.s2;
public class PowxN {
  public static void main(String[] args) {
       Solution solution = new PowxN().new Solution();
      final double pow = solution.myPow(2, 5);
      System.out.println("pow = " + pow);
  }
  //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
      double fastMul(double x, int n) {
          System.out.println(String.format("x = %s, n = %d", x, n));
          if(n == 0) {
              return 1.0;
          }
          double res = fastMul(x, n / 2);
          System.out.println(String.format("res = %s", res));
          return n % 2 == 0 ? res * res : res * res * x;
      }


      public double myPow(double x, int n) {
          return n >= 0 ? fastMul(x, n) : 1 / fastMul(x, n);
      }
  }
//leetcode submit region end(Prohibit modification and deletion)

}